Java IText
- Java PDF Generation with IText
- Getting Started with IText PDF API for Java
- Java IText: Document
- Java IText: Chunk
- Java IText: Phrase
- Java IText: Paragraph
- Java IText: Chapter + Section
- Java IText: Anchor (link)
- Java IText: List
- Java IText: Table
- Java IText: Image
- Java IText: Superscript + Subscript
- Java IText: Underline + Strikethrough
- Java IText: Font
- Java IText: Modifying Existing PDF Documents
Java IText: Chapter + Section
Jakob Jenkov |
You can add chapters and sections to a PDF document using IText. Chapters
are represented by the class com.itextpdf.text.Chapter. Sections
are represented by the class com.itextpdf.text.Section.
Here is a simple code example:
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ChapterSectionExample {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("ChapterSection.pdf"));
document.open();
Paragraph paragraph = new Paragraph();
paragraph.add(new Phrase("This is a chapter."));
Chapter chapter = new Chapter(paragraph, 1);
Section section1 = chapter.addSection("This is section 1", 2);
Section section2 = chapter.addSection("This is section 2", 2);
document.add(chapter);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Here is what the generated document looks like:
![]() |
| An IText Chapter and Section example |
| Tweet | |
Jakob Jenkov | |










